package com.example.main;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main07 {

    private static final Pattern patternBody = Pattern.compile("\\{([^}]*)\\}");

    public static void main(String[] args) {
//        String dir="/usr/local/{YYYYMM(dd)(6th calendar of last month)}";
        String dir="/usr/local";
        String s = parseBody(dir);
        System.out.println(s);
    }

    public static String parseBody(String body) {
        String text = body;
        Matcher m = patternBody.matcher(text);
        while (m.find()) {
            String parse = m.group();
            // 解析大括号里的内容
            String parseBody = parse.substring(1).substring(0, parse.length() - 2);

            // 1. 设置显示格式
            String showFormat = parseBody;
            // 日期显示格式 去掉小括号后的内容
            if (parseBody.indexOf("(") >= 0) {
                showFormat = showFormat.substring(0, parseBody.indexOf("("));
            }
            if (parseBody.indexOf("(dd)") >= 0 || parseBody.indexOf("(DD)") >= 0) {
                showFormat += "dd";
            }

            showFormat = showFormat.replace("DD", "dd");
            showFormat = showFormat.replace("24mil:mm:ss", "HH:mm:ss");

            // 2. 设置时间
            Date date = new Date();
            Calendar now = Calendar.getInstance();
            int lastLeftIndex = parseBody.lastIndexOf("(");
            int lastsRightIndex = parseBody.lastIndexOf(")");
            if (lastLeftIndex >= 0 && lastsRightIndex >= 0) {
                // 解析小括号里内容
                String changeFormat = parseBody.substring(lastLeftIndex + 1, lastsRightIndex);
                boolean status = changeFormat.contains(",");
                if(status){
                    changeFormat=changeFormat.substring(0,changeFormat.indexOf(","));

                }else{
                    changeFormat = parseBody.substring(lastLeftIndex+1, lastsRightIndex);
                }
                changeFormat = changeFormat.replaceAll(" ", "");
                switch (changeFormat) {
                    case "lastmonth":
                        // 上个月
                        now.add(Calendar.MONTH, -1);
                        date = now.getTime();
                        break;
                    case "lastdayoflastmonth":
                        now.add(Calendar.MONTH, -1);
                        now.set(Calendar.DATE, now.getActualMaximum(Calendar.DATE));
                        date = now.getTime();
                        break;
                    case "lastyear":
                        now.add(Calendar.MONTH, -1);
                        now.add(Calendar.YEAR, -1);
                        date = now.getTime();
                        break;

                }


                // 如果有两个小括号
                if (parseBody.indexOf("(") != parseBody.lastIndexOf("(")) {
                    // 上个月指定天数
                    if (changeFormat.indexOf("thcalendaroflastmonth") >= 0) {
                        now.add(Calendar.MONTH, -1);
                    }
                    // 获取具体天数
                    String day = changeFormat.substring(0, changeFormat.indexOf("th"));
                    int dayInt = Integer.parseInt(day);
                    int daySum = now.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (dayInt > daySum) {
                        dayInt = daySum;
                    }
                    now.set(Calendar.DATE, dayInt);
                    date = now.getTime();

                }

            }

            // 转换日期
            SimpleDateFormat f = new SimpleDateFormat(showFormat);
            String dateStr = f.format(date);

            text = text.replace(parse, dateStr);
        }
        return text;
    }
}
